package com.budwk.app.miniapp.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.budwk.app.fetchdata.models.HttpClientUtil;
import org.apache.commons.lang3.StringUtils;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.concurrent.ConcurrentHashMap;

public class AddressUtils {
    /**
     * 根据经纬度获取地址信息
     * @param longitude 经度
     * @param latitude 纬度
     * @return 地址信息，包括省、市、区等
     */
    public static JSONObject getAddressInfo(String longitude, String latitude) {
        if(longitude.equals("")||latitude.equals("")||longitude==null||latitude==null)
        {
            return new JSONObject();
        }
        String urlString = "http://api.tianditu.gov.cn/geocoder?postStr={'lon':" + longitude + ",'lat':" + latitude + ",'ver':1}&type=geocode&tk=25a77f643d94d6acbd6c1b79cfeeda69";
        System.out.println(urlString);
        StringBuilder result = new StringBuilder();
        BufferedReader in = null;
        try {
            URL url = new URL(urlString);
            URLConnection connection = url.openConnection();
            connection.setRequestProperty("Accept-Charset", "UTF-8");
            connection.setConnectTimeout(5000);
            connection.connect();
            in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
            String line;
            while ((line = in.readLine()) != null) {
                result.append(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return JSONObject.parseObject(result.toString());
    }

    //根据地名查经纬度
    public static ConcurrentHashMap getLatAndLngByAddr(String addr) {
        ConcurrentHashMap hmap=new ConcurrentHashMap();
        try {
            String result = "";


            addr = addr.replace(" ", "").replace("#", "").replace("中国", "");
            String queryStr = "http://api.tianditu.gov.cn/geocoder?ds=%7B'keyWord':'" + addr + "'%7D&tk=" + "25a77f643d94d6acbd6c1b79cfeeda69";
            /**
             *HttpClientUtil获取路径：
             *https://blog.csdn.net/weixin_40052298/article/details/123347111?spm=1001.2014.3001.5501
             */
            String info = HttpClientUtil.get(queryStr);
            System.out.println(info);
            if (StringUtils.isNotBlank(info)) {
                JSONObject resultJson = JSON.parseObject(info);
                if (!"无结果".equals(resultJson.get("msg"))) {
                    JSONObject locationObj = (JSONObject) resultJson.get("location");
                    //纬度
                    String lat = locationObj.get("lat") + "";
                    //经度
                    String lng = locationObj.get("lon") + "";
                    hmap.put("lng",lng);
                    hmap.put("lat",lat);



                    result = "经度：" + lng + "\n纬度：" + lat;
                }
            }/*"经度：106.566386 纬度：26.620078"*/
            return hmap;
        } catch (Exception e) {
            return  hmap;
        }
    }
}
